{extend name="main"}
{block name="body"}
<div class="bg-white p-3">
	<div class="pb-3">
        <el-button
            type="warning"
            size="mini"
            @click="updateFileAjax">
            更新缓存
        </el-button>
	</div>
	<div class="table-responsive">
		<el-table
			ref="tableList"
	      	:data="tableData"
    		style="width: 100%;"
    		header-cell-class-name="table-header-DDE6FB"
		    row-key="id"
		    lazy
		    :load="initHtml"
		    :tree-props="defaultProps">
		    <el-table-column
		      prop="title"
		      label="地区">
		    </el-table-column>
		    <el-table-column
		      prop="alias"
		      label="别名"
		      width="200">
		    </el-table-column>
		    <el-table-column
		      label="行政等级"
		      width="120">
		      <template slot-scope="scope">
		      	<span v-if="scope.row.level === 1" class="text-primary">省</span>
		      	<span v-if="scope.row.level === 2" class="text-danger">市</span>
		      	<span v-if="scope.row.level === 3" class="text-success">区/县</span>
		      </template>
		    </el-table-column>
		    <el-table-column
		      label="热门"
		      width="120">
		      <template slot-scope="scope">
		      	<transition>
		      		<el-button type="success" size="minism"
		      		v-if="scope.row.hot==1 || scope.row.hot==2" 
		      		@click="setAreaHot(scope.row);" 
		      		:loading="scope.row.hot==2">热门</el-button>
		      		<el-button type="info" size="minism" 
		      		v-else="scope.row.hot==0 || scope.row.hot==2" 
		      		@click="setAreaHot(scope.row);" 
		      		:loading="scope.row.hot==2">非热门</el-button>
				</transition>
		      </template>
		    </el-table-column>
		    <el-table-column
		      label="状态"
		      width="120">
		      <template slot-scope="scope">
		      	<transition>
		      		<el-button type="success" size="minism"
		      		v-if="scope.row.status==1 || scope.row.status==2" 
		      		@click="setAreaStatus(scope.row);" 
		      		:loading="scope.row.status==2">正常</el-button>
		      		<el-button type="info" size="minism" 
		      		v-else="scope.row.status==0 || scope.row.status==2" 
		      		@click="setAreaStatus(scope.row);" 
		      		:loading="scope.row.status==2">禁用</el-button>
				</transition>
		      </template>
		    </el-table-column>
		    <el-table-column
		      prop="sort"
		      label="排序"
		      width="80">
		    </el-table-column>
		    <el-table-column
		      label="/"
		      width="120">
		      <template slot-scope="scope">
		        <el-button
		            type="primary"
		            plain
		            round
		            size="minism"
		            @click="window.open('/area/edit/'+scope.row.id)">
		            编辑
		        </el-button>
		        <el-button
		            type="danger"
		            plain
		            round
		            size="minism"
		            @click="Delete(scope)">
		            删除
		        </el-button>
		      </template>
		    </el-table-column>
		</el-table>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
	var vm=new Vue({
		el:'#app',
		data:{
			view:XYBase.view,
			search:{
				id:0,
			},
			tableData:[],
	        defaultProps: {
	          children: 'children',
	          hasChildren: 'hasChildren'
	        }
		},
		watch:{
		    view:XYBase.viewWatch,
		},
		created(){
			XYBase.created(this);
		},
	    methods: {
	    	setAreaHot(data){
	    		if (data.hot==2) return;
	    		var hot=1,oldHot=data.hot;
	    		if (data.hot) hot=0;
	    		data.hot=2;
	    		$.getJSON('/area/setAreaHot/'+data.id+'/'+hot,function(ret){
	    			data.hot=oldHot;
	    			if (ret) {
	    				if (ret.code === 0) {
	    					data.hot=hot;
	    				}else{
	    					XYBase.msg(ret.msg);
	    				}
	    			}else{
						XYBase.error(XYBase.config.system_network_error);
	    			}
	    		});
	    	},
	    	setAreaStatus(data){
	    		if (data.status==2) return;
	    		var status=1,oldStatus=data.status;
	    		if (data.status) status=0;
	    		data.status=2;
	    		$.getJSON('/area/setAreaStatus/'+data.id+'/'+status,function(ret){
	    			data.status=oldStatus;
	    			if (ret) {
	    				if (ret.code === 0) {
	    					data.status=status;
	    				}else{
	    					XYBase.msg(ret.msg);
	    				}
	    			}else{
						XYBase.error(XYBase.config.system_network_error);
	    			}
	    		});
	    	},
	    	updateFileAjax() {
		        this.$confirm('即将更新地区缓存, 是否继续?', '提示', {
		          confirmButtonText: '继续',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		    		const loading=XYBase.openloading();
		    		$.getJSON('/area/updateFileAjax',function(ret) {
		    			loading.close();
		    			if(ret){
		    				if(ret.code === 0){
		    					XYBase.success(ret.msg);
		    				}else{
		    					XYBase.msg(ret.msg);
		    				}
		    			}else{
							XYBase.error(XYBase.config.system_network_error);
		    			}
		    		});
		        }).catch(() => {
					XYBase.msg('已取消');
		        });
	    	},
	    	Delete(scope){
	    		var _this=this;
		        _this.$confirm('确定将《'+scope.row.title+'》删除吗？此操作不可逆，请谨慎操作', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		        	var loading=XYBase.openloading();
		    		$.getJSON('/area/delete/'+scope.row.id,function(ret){
		    			loading.close();
		    			if (ret) {
		    				if (ret.code === 0) {
		    					window.location.reload();
		    				}else{
		    					XYBase.msg(ret.msg);
		    				}
		    			}else{
							XYBase.error(XYBase.config.system_network_error);
		    			}
		    		});
		        }).catch(() => {
    				XYBase.msg('已取消');
		        });
	    	},
	      	initHtml(tree, treeNode, resolve) {
	      		var _this=this;
		      	if (typeof tree != "undefined")
		      		_this.search.id=tree.id;
				$.ajax({
					url:'',
					type:'get',
					data:_this.search,
					dataType:'json',
					error:function(err){
						XYBase.error(XYBase.config.system_network_error);
					},
					success:function(ret){
						if (ret) {
							if (ret.code === 0) {
								if (!_this.search.id) {
									_this.tableData=ret.data;
								}else{
									resolve(ret.data);
								}
							}else{
								XYBase.msg(ret.msg);
							}
						}else{
							XYBase.error(XYBase.config.system_network_error);
						}
					}
				});
			},
	    }
	});
	$(function(){
		vm.initHtml();
	});
</script>
{/block}